<?php

namespace App\Admin\Controllers;

use App\Models\PayLog;
use App\Models\Client;
use App\Models\StatisticsConsume;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class StatisticsConsumeController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('风控数据统计')
            ->description('数据列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new StatisticsConsume);

        $grid->date('日期');
        $grid->model()->orderBy('date', 'desc');
        $grid->column('client.name', '名称')->display(function($val){
            return $val;
        });
        $grid->verify('实名认证数');
        $grid->mobile('手机认证数');
        $grid->bankcard('银行卡认证数');
        $grid->access('准入分认证数');
        $grid->confidence('置信分认证数');
        $grid->message('短信认证数');
        $grid->risk('风控费认证');

        $grid->filter(function ($filter) {
            // 禁用id查询框
            $filter->disableIdFilter();

            $filter->column(1 / 2, function ($filter) {
                $filter->equal('name', '名称')->select(Client::all()->pluck('name', 'sign'));
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->between('date', '日期')->datetime();
            });
        });

        $grid->disableExport();
        $grid->disableRowSelector();
        $grid->disableActions();
        $grid->disableCreateButton();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(PayLog::findOrFail($id));

        $show->id('Id');
        $show->client_url('Client url');
        $show->status('Status');
        $show->pay_type('Pay type');
        $show->appid('Appid');
        $show->mch_id('Mch id');
        $show->bank_type('Bank type');
        $show->cash_fee('Cash fee');
        $show->post_price('Post price');
        $show->fee_type('Fee type');
        $show->is_subscribe('Is subscribe');
        $show->nonce_str('Nonce str');
        $show->openid('Openid');
        $show->out_trade_no('Out trade no');
        $show->result_code('Result code');
        $show->return_code('Return code');
        $show->time_end('Time end');
        $show->total_fee('Total fee');
        $show->trade_type('Trade type');
        $show->transaction_id('Transaction id');
        $show->err_code('Err code');
        $show->err_code_des('Err code des');
        $show->order_at('Order at');
        $show->paid_at('Paid at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new PayLog);

        $form->text('client_url', 'Client url');
        $form->switch('status', 'Status')->default(1);
        $form->switch('pay_type', 'Pay type');
        $form->text('appid', 'Appid');
        $form->text('mch_id', 'Mch id');
        $form->text('bank_type', 'Bank type');
        $form->number('cash_fee', 'Cash fee');
        $form->decimal('post_price', 'Post price')->default(0.00);
        $form->text('fee_type', 'Fee type');
        $form->text('is_subscribe', 'Is subscribe');
        $form->text('nonce_str', 'Nonce str');
        $form->text('openid', 'Openid');
        $form->text('out_trade_no', 'Out trade no');
        $form->text('result_code', 'Result code');
        $form->text('return_code', 'Return code');
        $form->number('time_end', 'Time end');
        $form->decimal('total_fee', 'Total fee')->default(0.00);
        $form->text('trade_type', 'Trade type');
        $form->text('transaction_id', 'Transaction id');
        $form->text('err_code', 'Err code');
        $form->text('err_code_des', 'Err code des');
        $form->number('order_at', 'Order at');
        $form->number('paid_at', 'Paid at');

        return $form;
    }
}
